/**
 * Created by Administrator on 2014/12/22.
 */

var SucOrderNews = (function(){

	var INTERVAL_TIME = 5 * 60 * 1000;
	var newsSlider = null;

	function ajax(fns){
		fns = fns || {};
		var url = G_CONFIG.url.successOrderList;
		var fn = new Function();
		var loading = fns.loading || fn;
		var removeLoading = fns.removeLoading || fn;
		var success = fns.success || fn;
		var empty = fns.empty || fn;
		var timeout = fns.timeout || fn;
		var ttimeout = G_CONFIG.ajaxtimeout;
		var error = fns.error || fn;
		loading();
		$.ajax({
			url : url,
			type : "GET",
			dataType : "JSON",
			timeout : ttimeout,
			data : {
				action : "RealTimeOrder"
			},
			success : function(res){
				removeLoading();
				if(res && res.length){
					success(res);
				}else{
					empty(res);
				}
			},
			error : function(xhr,txt){
				removeLoading();
				if(txt == "timeout"){
					timeout();
				}else{
					error();
				}
			}
		})
	}

	function initGet(){ //初始化请求
		var wrap = $("#sucOrderList");
		var listUl = wrap.find(".listUl");
		var loadingTxt = '<li class="loading statusTxt">加载中...</li>';
		var emptyTxt = '<li class="empty statusTxt">返回数据为空...</li>';
		var timeoutTxt = '<li class="timeout statusTxt">请求超时，请<a class="retryBtn" style="margin-left:3px;" href="#">重试</a></li>';
		var errorTxt = '<li class="error statusTxt">请求出错，请<a class="retryBtn" style="margin-left:3px;" href="#">重试</a></li>';
		ajax({
			loading : function(){
				listUl.html(loadingTxt);
			},
			removeLoading : function(){
				listUl.html("");
				_setInterval();
			},
			success : function(res){
				var html = build(res);
				listUl.html(html);
				setTimeout(function(){
					newsSlider = new S($("#newsSlideStage"));
				},0)
			},
			empty : function(){
				listUl.html(emptyTxt);
			},
			timeout : function(){
				listUl.html(timeoutTxt);
			},
			error : function(){
				listUl.html(errorTxt);
			}
		})
	}
	function update(){
		var wrap = $("#sucOrderList");
		var listUl = wrap.find(".listUl");
		ajax({
			success : function(res){
				listUl.html(build(res));
				setTimeout(function(){
					newsSlider.refresh();
				},0)
			}
		})
	}
	function _setInterval(){
		setInterval(update,INTERVAL_TIME);
	}
	function build(res){
		var html = "";
		for(var i in res){
			var item = res[i];
			html += '<li><div class="liCon"><span class="t">'+item["ordertime"]+'</span><span class="user">'+item["ordername"]+'</span><span class="suc">成功预订</span><a href="'+G_CONFIG.url.jumpToDetail+'?lid='+item["lid"]+'" target="_blank" class="pn">'+item["ltitle"]+' '+item["ttitle"]+'</a></span></div></li>';
		}
		return html;
	}
	var S = (function(){

		function newsSlider(container,config){
			var config = this.config = $.extend({},newsSlider.defaults,config);
			this.container = container;
			this.listUl = container.children();
			this.step = config.step || this.listUl.children().first().outerHeight(true);
			this.slideSpeed = config.speed;
			this.slideInterval = config.interval;
			this.dir = config.dir.toLowerCase();
			this.timer = null;
			this.init();
		}
		newsSlider.defaults = {
			dir : "v",   //方向  h-横向   v-竖向
			speed : 400, //滚动速度
			interval : 2 //滚动间隔 单位秒
		};
		newsSlider.prototype = {
			init : function(){
				var self = this;
				this.container.hover(function(){
					self.stop();
				},function(){
					self.begin();
				})
				this.timer = setTimeout(function(){
					self.anim();
				},this.slideInterval*1000);
			},
			anim : function(){
				var self = this;
				var dir = this.dir == "v" ? "top" : "left";
				var step = this.step;
				var slideInterval = this.slideInterval;
				var speed = this.slideSpeed;
				var listUl = this.listUl;
				var css = {};
				css[dir] = -1*step;
				listUl.stop().animate(css,speed,function(){
					listUl.css("top",0).children().first().appendTo(listUl);
					clearTimeout(self.timer);
					self.timer = setTimeout(function(){
						self.anim();
					},slideInterval*1000);
				})
			},
			refresh : function(){
				var self = this;
				var css = this.dir == "v" ? {top:0} : {left:0};
				this.listUl = this.container.children();
				this.listUl.css(css).stop();
				this.timer = setTimeout(function(){
					self.anim();
				},this.slideInterval*1000)
			},
			stop : function(){
				this.listUl.stop();
				clearTimeout(this.timer);
			},
			begin : function(){
				var self = this;
				this.timer = setTimeout(function(){
					self.anim();
				},1000)
			}
		}

		return newsSlider;

	})();
	function init(){
		initGet();
	}

	return{
		init : init
	}

})();

$(function(){
	SucOrderNews.init();
})
